package com.easyardemo.tools.httptools;

import com.easyardemo.app.HttpApi;
import com.easyarlibrary.util.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtil {


    private static HttpUtil httpUtil;
    private OkHttpClient client;

    private HttpUtil() {
        client = new OkHttpClient();
    }

    public static HttpUtil getInstance() {

        synchronized (HttpUtil.class) {
            if (httpUtil == null) {
                synchronized (HttpUtil.class) {
                    httpUtil = new HttpUtil();
                }
            }
            return httpUtil;
        }
    }

    public void get(String url, final Icallback icallback) {
        //构造Request对象
        //采用建造者模式，链式调用指明进行Get请求,传入Get的请求地址
        Request request = new Request.Builder().get().url(url).build();
        Call call = client.newCall(request);
        //异步调用并设置回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                LogUtil.d(e.toString());
                if (icallback != null) {
                    icallback.onError(e);
                }
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String responseStr = response.body().string();
                LogUtil.d(responseStr);
                if (icallback != null) {
                    icallback.onSuccess(responseStr);
                }
            }
        });
    }


    public void postForm(String url, Map<String, String> parment, final Icallback icallback) {
        OkHttpClient client = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        Iterator<Map.Entry<String, String>> iterator = parment.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            builder.addFormDataPart(entry.getKey(), entry.getValue());
        }

        RequestBody requestBody = builder.build();

        final Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        //异步调用并设置回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (icallback != null) {
                    icallback.onError(e);
                }
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String responseStr = response.body().string();
                if (icallback != null) {
                    icallback.onSuccess(responseStr);
                }
            }
        });
    }


    public void postJson(String url, JSONObject paramdata, final Icallback icallback) {
        JSONObject param = new JSONObject();
        try {
            param.put("bizCode", HttpApi.GET_DOWNLOAD_BIZCODE);
            param.put("action", "");
            param.put("data", paramdata);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=utf-8"),
                param.toString());

        LogUtil.d("url---->"+url);
        LogUtil.d(param.toString());
        final Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        //异步调用并设置回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtil.d(e.toString());
                if (icallback != null) {
                    icallback.onError(e);
                }
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String responseStr = response.body().string();
                LogUtil.d(responseStr);
                if (icallback != null) {
                    icallback.onSuccess(responseStr);
                }
            }
        });
    }

}
